package eco2automotive.gui.components;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import eco2automotive.data.HashParameters;
import eco2automotive.data.Parameter;

/** JPanel, display the calculated values when starts the monitoring.
  * @version 1.0
  * @autor Fco. Javier Sánchez Esparza
 */
public class Panel_MONITOR extends JPanel{
 private JTable table;
 
  private JScrollPane panelScroll;
   private String titColumn[];
  private String dataColumn[][];
  private int NUM_OF_ROWs;
  HashParameters h; 
  
  /**
   *
   * @param h HashParameters object
   */
  public Panel_MONITOR(HashParameters h){
  this.setLayout( new BorderLayout()); 
    this.h=h;
    CreateColumns();
    LoadData(h);    
    this.insertFromHash(h);
    table = new JTable( dataColumn,titColumn);
    table.setShowHorizontalLines( true );
    table.setRowSelectionAllowed( true );    	        
    Font font=new Font("Arial",Font.BOLD,10);
    table.setFont(font);
    table.sizeColumnsToFit(9);                				
    panelScroll = new JScrollPane( table );
    		panelScroll.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    	panelScroll.setBorder(new javax.swing.border.TitledBorder("Parameters"));    	
        this.add(panelScroll);    	
 
        
  }  
  /**
   * Create the name of the colums to display.
   */
  public void CreateColumns() {
    titColumn = new String[10];
 	 
	 titColumn[0]="Key";
         titColumn[1]="Mode/PID";
         titColumn[2]="Description";
         titColumn[3]="Bytes/N.of PIDs";         
         titColumn[4]="Formula";   
         titColumn[5]="Mask";            
	 titColumn[6]="Min";
	 titColumn[7]="Max";
         titColumn[8]="#Calc.";
	 titColumn[9]="Units";                    
    }
/**
 * Load the data from a HashParamter object to specify the num of rows
 * @param h
 */
public void LoadData(HashParameters h) {
    /*
     Num of rows?... 
     */    
    int numof_rows=h.getHash().size();    
 Iterator it = h.getHash().entrySet().iterator();    
 while (it.hasNext()) { 
        Map.Entry e = (Map.Entry)it.next();
        Parameter p = (Parameter)e.getValue();       
            if(p.getNumberofPids()>1)
                numof_rows=numof_rows+(p.getNumberofPids()-1);   
    }
 
        dataColumn = new String[numof_rows][10];
    
}

/**
 * Update the data in the columname named "#Calc.", 
 * find key in row who columname is named "Key", eg: key=011D.0 column=key
 * @param key
 * @param data
 */
public void updateCalculatedValue(String key, String data) 
{
for(int j=0;j<table.getColumnCount();j++)
   if(this.table.getColumnName(j).equals("Key"))
    for(int i=0;i<NUM_OF_ROWs;i++)
                if( dataColumn[i][j].equals(key))
                    for(int z=0;z<table.getColumnCount();z++) 
                        if(this.table.getColumnName(z).equals("#Calc.")) 
                            {
                    System.out.println("COINCIDENCIA EN TABALA FILA i:"+i+"COLUMNA j:"+z);                    
                    dataColumn[i][z]=data;this.updateUI();
                    table.setRowSelectionInterval(i,i);
                }
}

/**
 * Update by "mode" and "pid"+[id of PIDValue] 
 * @param data
 * @param mode
 * @param pid
 * @param num_of_value
 */
public void updateData(String data,String mode, String pid, int num_of_value){
 String key=mode.concat(pid).concat(".").concat(String.valueOf(num_of_value));//011D.0
 updateCalculatedValue(key,data) ;
}

/**
 * Insert all data from HasParameter
 * @param h
 */
public void insertFromHash(HashParameters h)
{

      NUM_OF_ROWs=0;

      Parameter insertar;
        
try{ 
    HashMap mapResult = new LinkedHashMap();
    Map miMap= new HashMap();
    miMap=h.getHash();
    Map mapOrdenado = new TreeMap(miMap);
    Set ref = mapOrdenado.keySet();
   Iterator itr = ref.iterator();
 
   while (itr.hasNext()) {
        Parameter p = (Parameter)h.getObject((String)itr.next());
        
for(int i=0;i<=p.getNumberofPids()-1;i++) 
           {

 /* Key */             dataColumn	[NUM_OF_ROWs][0]=p.getOperationMode()+p.getPidKey()+"."+i;
 /* "Mode/PID" */      dataColumn	[NUM_OF_ROWs][1]=p.getOperationMode()+"-"+p.getPidKey();					
/* Description */      dataColumn	[NUM_OF_ROWs][2]=p.getDescription();
/* Bytes/Num of PIDs */dataColumn	[NUM_OF_ROWs][3]=String.valueOf(p.getBytesReturned())+"-"+String.valueOf(p.getNumberofPids());
/* Formula*/              dataColumn	[NUM_OF_ROWs][4]=p.getPID(i).getFormula();
/*Mask*/               dataColumn	[NUM_OF_ROWs][5]=p.getPID(i).getMasc();
/*Min*/            dataColumn	[NUM_OF_ROWs][6]=Double.toString(p.getPID(i).getMinValue());
/*Max*/              dataColumn	[NUM_OF_ROWs][7]=Double.toString(p.getPID(i).getMaxValue());
                       dataColumn	[NUM_OF_ROWs][8]="-";
                       dataColumn	[NUM_OF_ROWs][9]=p.getPID(i).getUnits();         
NUM_OF_ROWs++;
}

}

}
catch(Exception e){	
	System.out.println( e );
      return;
}

	this.updateUI();		
			
}
}